package com.example.hanchen.yongkanggy.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModelProvider;

import com.example.hanchen.yongkanggy.BR;
import com.example.hanchen.yongkanggy.R;
import com.example.hanchen.yongkanggy.databinding.ActivityLoginBinding;
import com.example.hanchen.yongkanggy.databinding.ActivityMainBinding;
import com.example.hanchen.yongkanggy.entity.StatusCode;
import com.example.hanchen.yongkanggy.home.base.StatusBarDatabindingActivity;
import com.example.hanchen.yongkanggy.home.viewmodel.HomeViewModel;
import com.example.hanchen.yongkanggy.http.ConstantData;
import com.example.hanchen.yongkanggy.http.UserViewModel;
import com.example.hanchen.yongkanggy.utils.AesUtils;
import com.example.hanchen.yongkanggy.utils.Md5Utils;
import com.example.hanchen.yongkanggy.utils.PreferencesUtil;
import com.example.hanchen.yongkanggy.utils.SharePreferencesConsts;
import com.example.hanchen.yongkanggy.utils.TreeMapUtils;

import java.util.Map;
import java.util.TreeMap;

/*
 * 登录界面
 * */

public class LoginActivity extends StatusBarDatabindingActivity<ActivityLoginBinding, HomeViewModel> {

    private UserViewModel userViewModel;

    @Override
    protected int initLayoutID(Bundle savedInstanceState) {
        return R.layout.activity_login;
    }

    @Override
    public int initVariableId() {
        return BR.logomodel;
    }

    @Override
    protected void initView() {
        userViewModel = new ViewModelProvider(this).get(UserViewModel.class);

    }

    @Override
    protected void initData() {

        mDatabinding.tvLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                startActivity(new Intent(LoginActivity.this, HomeActivity.class));

                Map<String, String> unsortedMap = new TreeMap<>();
                try {
                    unsortedMap.put("password", AesUtils.encrypt(mDatabinding.ctvPassword.getText().toString()));
                    unsortedMap.put("phone", AesUtils.encrypt(mDatabinding.ctPhone.getText().toString()));
                } catch (Exception e) {
                    e.printStackTrace();
                }

                String treeMap = TreeMapUtils.getTreeMap(unsortedMap);
                /*避免拼接的数据被换行*/
                String replace = treeMap.replace("\n", "").replace("\r", "");
                String phones = null;
                String passwords = null;

                try {
                    phones = AesUtils.encrypt(mDatabinding.ctPhone.getText().toString()).replace("\n", "").replace("\r", "");
                    passwords = AesUtils.encrypt(mDatabinding.ctvPassword.getText().toString()).replace("\n", "").replace("\r", "");
                } catch (Exception e) {
                    e.printStackTrace();
                }


                String parameterSign = Md5Utils.getMD5String(replace + ConstantData.BUSINESSSECRETKEY);
                LiveData<StatusCode> userLiveData = userViewModel.clientuserlogin(phones, passwords,"1", parameterSign);
                userLiveData.observe(LoginActivity.this, user -> {
                    Log.i("JFOEJFIJE", "SDJFLDJJF  " + user.getCode() + "   " + user.getMsg());
                    try {
                        Toast.makeText(LoginActivity.this, user.getMsg(), Toast.LENGTH_SHORT).show();
                        if (user.getCode() == 301) {
                            return;
                        }

                        if (user.getCode() == 200) {
                            startActivity(new Intent(LoginActivity.this, HomeActivity.class));
                            PreferencesUtil.saveObject(SharePreferencesConsts.LOGINSMS, user);
                            finish();
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });

            }
        });

    }
}